package com.sdry.web.controller.cb;

import com.sdry.model.cb.ReportFormEntity;
import com.sdry.model.cb.TableDataInfo;
import com.sdry.model.jyy.Receive;
import com.sdry.model.llm.LlmCancellingStocks;
import com.sdry.model.lz.OutStockOrder;
import com.sdry.model.zc.ZcRejectsWarehouseEntity;
import com.sdry.service.cb.ReportFormService;
import com.sdry.utils.ExcelUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version: 1.0
 * @description: 报表接口
 * @author: cb
 * @create: 2019-12-13 14:10
 **/
@Controller
@RequestMapping("report")
public class InterfaceReport extends BaseController{

    @Autowired
    private ReportFormService reportFormService;

    public static Logger logger = LogManager.getLogger(ReportFormController.class);

    /**
     * 多外接口_日报表,多仓库版本 cb_2019年11月7日20:41:56
     * @return
     */
    @RequestMapping(value = "queryDateReport")
    @ResponseBody
    public TableDataInfo queryDateReport(
             Integer page , Integer pageSize,  String date,Long customer_id,String wareHouseId,String ma_id
    )  {

        try {
            logger.info("获取日报表列表,多仓库版本 cb_2019年11月7日20:41:56");
            String firsTime = reportFormService.getReportFormFirsTime();
            List<ReportFormEntity> list = reportFormService.queryDateReport(ma_id, date, customer_id, firsTime, wareHouseId,page,pageSize);
            return getDataTable(list,reportFormService.queryDateReportCount(ma_id, date, customer_id, firsTime, wareHouseId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取日报表列表报错");
            return getDataTable(new ArrayList<>(),100);
        }
    }

    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("exportallDateBalance")
    @ResponseBody
    public List<ReportFormEntity> exportallDateBalance(HttpServletResponse response , Integer page , Integer pageSize,  String date,Long customer_id,String wareHouseId,String ma_id) {
        String firsTime = reportFormService.getReportFormFirsTime();
        page = 1;pageSize = 10000;
        List<ReportFormEntity> list = reportFormService.queryDateReport(ma_id, date, customer_id, firsTime, wareHouseId,page,pageSize);
        return list;
    }

    /**
     * 月报表,多仓库版本 cb_2019年11月7日20:41:56
     * @return
     */
    @RequestMapping(value = "queryMonthReport")
    @ResponseBody
    public TableDataInfo queryMonthReport(
            String wareHouseId, Integer page , Integer pageSize, String ma_id, String date,Long customer_id
    ) {
        try {
            logger.info("获取月报表列表,多仓库版本 cb_2019年11月7日20:41:56 customer_id=" + customer_id + ",pageSize=" + pageSize + ",ma_id=" + ma_id + ",date=" + date + "customer_id" + customer_id);
            if (date.length() == 7) {
                date += "-01";
            }
            String firsTime = reportFormService.getReportFormFirsTime();
            List<ReportFormEntity> list = reportFormService.queryReportFormList(ma_id, date, customer_id, firsTime, wareHouseId, page, pageSize);
            Integer count = reportFormService.queryReportFormListCount(ma_id, date, customer_id, firsTime, wareHouseId);
            return getDataTable(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("cb_获取月报表列表出错,");
            return getDataTable(new ArrayList<>(), 100);
        }
    }

    /**
     * 导出Excel
     * @param
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/exportallReportFormList")
    @ResponseBody
    public List<ReportFormEntity> ExportallReportFormList( String wareHouseId, Integer page , Integer pageSize, String ma_id, String date,Long customer_id) {
        String firsTime = reportFormService.getReportFormFirsTime();
        List<ReportFormEntity> list = reportFormService.queryReportFormList(ma_id, date, customer_id, firsTime, wareHouseId, 1, 1000000);
       return list;
    }

    /**
     * 入库报表,多仓库版本 cb_2019年11月7日20:41:56
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "queryStoreInWareHouse")
    @ResponseBody
    public TableDataInfo queryStoreInWareHouse(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2,Long wareHouseId
    ) {
        try {
            logger.info("获取入库报表列表,多仓库版本 cb_2019年11月7日20:41:56,selectTime2="+selectTime2+",customer_id="+customer_id+",ma_id="+maName+",date="+date+"selectTime2="+selectTime2);
            List<Receive> list = reportFormService.queryStoreInWareHouse(selectTime,maName,date,customer_id,selectType,selectTime2,page,pageSize,wareHouseId);
            Integer count = reportFormService.queryStoreInWareHouseTotal(selectTime,maName,date,customer_id,selectType,selectTime2,wareHouseId);
            return getDataTable(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取入库报表列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }


    /**
     * 导出Excel
     * @param
     */
    @RequestMapping("/exportReportFormJinList")
    @ResponseBody
    public List<Receive> exportReportFormJinList(String selectType, String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2,Long wareHouseId) {
        List<Receive> list = reportFormService.queryStoreInWareHouse(selectTime,maName,date,customer_id,selectType,selectTime2,page,pageSize,wareHouseId);
       return list;
    }


    /**
     * 出库报表,多仓库版本 cb_2019年11月7日20:41:56
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "queryStoreOutWareHouse")
    @ResponseBody
    public TableDataInfo queryStoreOutWareHouse(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2,Long wareHouseId
    ) {
        try {
            logger.info("获取出库报表列表,多仓库版本 cb_2019年11月7日20:41:56,selectTime2="+selectTime2+",customer_id="+customer_id+",ma_id="+maName+",date="+date+"selectTime2="+selectTime2);
            List<OutStockOrder> list = reportFormService.queryStoreOutWareHouse(maName,date,customer_id,selectTime,selectType,selectTime2,page,pageSize,wareHouseId);
            Integer total = reportFormService.queryStoreOutWareHouseTotal(maName,date,customer_id,selectTime,selectType,selectTime2,wareHouseId);
            return getDataTable(list, total);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取出库报表列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }

    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/exportReportFormChungList")
    @ResponseBody
    public List<OutStockOrder> exportReportFormChungList(HttpServletResponse response ,String selectType,
                                          String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2,Long wareHouseId) throws UnsupportedEncodingException, ParseException{
        List<OutStockOrder> list = reportFormService.queryStoreOutWareHouse(maName,date,customer_id,selectTime,selectType,selectTime2,page,pageSize,wareHouseId);
       return list;
    }


    /**
     * 退库报表,多仓库版本  cb_2019年11月7日19:07:43
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "queryStoreReturnWareHouse")
    @ResponseBody
    public TableDataInfo queryStoreReturnWareHouse(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String dateTime2,Long wareHouseId
    ) {

        try {
            logger.info("获取退库报表列表，多仓库版本  cb_2019年11月7日19:07:43,+dateTime2="+dateTime2+",customer_id="+customer_id+ ",ma_id="+maName+",date="+date);
            List<LlmCancellingStocks> list = reportFormService.queryStoreReturnWareHouse(selectTime,maName,date,customer_id,selectType,dateTime2,page,pageSize,wareHouseId);
            Integer total = reportFormService.queryStoreReturnWareHouseTotal(selectTime,maName,date,customer_id,selectType,dateTime2,wareHouseId);
            return getDataTable(list, total);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取退库报表列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }
    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/exportReportFormReTurnList")
    @ResponseBody
    public List<LlmCancellingStocks>  ExportReportFormReTurnList(HttpServletResponse response , String selectType,
                                           String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String dateTime2,Long wareHouseId) throws UnsupportedEncodingException, ParseException{
        List<LlmCancellingStocks> list = reportFormService.queryStoreReturnWareHouse(selectTime,maName,date,customer_id,selectType,dateTime2,page,pageSize,wareHouseId);
        return list;
    }


    /**
     * 不良库报表,多仓库版本cb-2019年11月7日20:26:12
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "queryStoreRejectsWareHouse")
    @ResponseBody
    public TableDataInfo queryStoreRejectsWareHouse(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2,Long wareHouseId
    )  {
        try {
            logger.info("获取不良库报表列表,多仓库版本cb-2019年11月7日20:26:12,selectTime2="+customer_id+",customer_id="+pageSize+ ",ma_id="+maName+",date="+date);
            List<ZcRejectsWarehouseEntity> list = reportFormService.queryStoreRejectsWareHouse(selectTime,maName,date,customer_id,selectType,selectTime2,page,pageSize,wareHouseId);
            Integer total = reportFormService.queryStoreRejectsWareHouseTotal(selectTime,maName,date,customer_id,selectType,selectTime2,wareHouseId);
            return getDataTable(list, total);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取不良库报表列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }
    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/exportReportFormReGeLIList")
    @ResponseBody
    public List<ZcRejectsWarehouseEntity> ExportReportFormReGeLIList(
            String selectType,String selectTime,
            Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2,Long wareHouseId) {
        List<ZcRejectsWarehouseEntity> list = reportFormService.queryStoreRejectsWareHouse(selectTime,maName,date,customer_id,selectType,selectTime2,page,pageSize,wareHouseId);
        return list;
    }
}
